/*
 * Copyright (c) 2025 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#ifndef RESOURCE_H
#define RESOURCE_H

#include <stdlib/stdint.h>
#include <tools/list.h>

#define RES_PRIORITY(res) (((resource_t *)res)->s_owner->priority)
typedef struct task_control_block tcb_t;

/**
 * @brief  resources that can cause congestion
 *
 */
typedef enum resource_type {
    RESOURCE_NULL = 0,
    RESOURCE_MUTEX,
    RESOURCE_COND,
    RESOURCE_NTFN,
    RESOURCE_TASK,
    RESOURCE_ENDPONIT,
    RESOURCE_SEM,
    RESOURCE_SIG,
} res_type_t;

typedef struct resource {
    list_t     s_list;     // resource list
    list_t     s_hlist;    // shared ownership list, indicates that this s_ptr will be held by multiple threads
    tcb_t     *s_owner;    // owner of the resource
    void      *s_ptr;      // resource pointer
    uint64_t   s_extra;    // extended attribute,
    res_type_t s_type;     // resource type
} resource_t;

void resource_init(resource_t *res, void *ptr, res_type_t type);
void task_delete_resource_deal(tcb_t *task);
void resource_set_holder(resource_t *res, tcb_t *task);
void resource_attach_waitlist(resource_t *res, void *ptr);
void resource_timeout_wake(tcb_t *task);

void   resource_prilist_add(list_t *list, resource_t *res);
tcb_t *resource_prilist_first(list_t *list);
tcb_t *resource_prilist_next(list_t *list);
#endif
